package com.billow.net.rtmp
{
	import flash.utils.Dictionary;
	
	internal class RtmpStream
	{
		private var _id:uint;
		private var _map:Dictionary;
		private var _publishInfo:RtmpStreamInfo;
		
		public function RtmpStream()
		{
			_id = 0;
			_map = new Dictionary();
		}
		
		public function getStreamID():uint {return _id = _id<0xFFFF?_id+1:1;}
		
		
		
		
		/**
		 * 广播 
		 * @param packet
		 * 
		 */		
		public function broadcastStream(packet:RtmpPacket):void
		{
			var playStreamIDs:Vector.<uint> = _map[packet.streamChannel].playStreamIDs;
			
			for (var i:int=0;i<playStreamIDs.length;i++)
			{
				var id:uint = playStreamIDs[i];
				packet.streamChannel = playStreamIDs[i];
				_map[id].client.sendPacket(packet);
			}
		}
		
		
		
		/**
		 * 创建 
		 * 
		 */ 
		public function onCreateStream(client:ClientControl ,packet:RtmpPacket):void
		{
			var responseID:uint = packet.readObject() as uint;
			var streamID:uint = getStreamID();
			_map[streamID] = new RtmpStreamInfo();
			_map[streamID].client = client;
			client.sendPacket(RtmpPacketManage.createStreamMessage(responseID, streamID));
		}
		
		
		
		
		
		/**
		 * 发布 
		 * 
		 */ 
		public function onPublish(client:ClientControl ,packet:RtmpPacket):void
		{
			var responseID:uint = packet.readObject() as uint;
			var nullValue:Object = packet.readObject();
			var streamName:String = packet.readObject() as String;
			var type:String = packet.readObject() as String;
			var streamID:uint = packet.streamChannel;
			
			_map[streamID].streamName = streamName;
			_map[streamID].publish = true;
			_map[streamID].playStreamIDs = new Vector.<uint>();
			
			_publishInfo = _map[streamID];
			
			
			for(var id:String in _map)
			{
				if(_map[id].streamName == streamName && _map[id].publish == false)
				{
					_map[id].publishStreamID = streamID;
					_map[streamID].playStreamIDs.push(uint(id));
				}
			}
			
			
			//如果有同名流，并且正在播放，则发送消息badName
			for(var id2:String in _map)
			{
				if(uint(id2) != streamID && _map[id2].streamName == streamName && _map[id2].publish == true)
				{
					client.sendPacket(RtmpPacketManage.streamPublishBadNameMessage(responseID, packet.streamChannel));
					return;
				}
			}
			
			//允许客户端发送视频
			client.sendPacket(RtmpPacketManage.streamPublishStartMessage(responseID, packet.streamChannel));
		}
		
		
		/**
		 *  
		 * @param client
		 * 
		 */		
		public function closeCheck(client:ClientControl):void
		{
			if(_publishInfo!=null && client == _publishInfo.client)
			{
				_publishInfo.streamName = "";
				_publishInfo.publish = false;
				_publishInfo.publishStreamID = 0;
			}
		}
		
		
		
		/**
		 * 播放 
		 * 
		 */ 
		
		public function onPlay(client:ClientControl ,packet:RtmpPacket):void
		{
			var responseID:uint = packet.readObject() as uint;
			var nullValue:Object = packet.readObject();
			var streamName:String = packet.readObject() as String;
			var streamID:uint = packet.streamChannel;
			
			_map[streamID].streamName = streamName;
			_map[streamID].publish = false;
			
			for(var id:String in _map)
			{
				if(_map[id].streamName == streamName && _map[id].publish == true)
				{
					_map[id].playStreamIDs.push(streamID);
					_map[streamID].publishStreamID = uint(id);
				}
			}
			
			
			client.sendPacket(RtmpPacketManage.streamPlayResetMessage(responseID, packet.streamChannel));
			client.sendPacket(RtmpPacketManage.streamPlayStartMessage(responseID, packet.streamChannel));
		}
		
		
		
		/**
		 * 关闭视频 
		 * 
		 */
		
		public function onCloseStream(client:ClientControl ,packet:RtmpPacket):void
		{
			var responseID:uint = packet.readObject() as uint;
			var streamID:uint = packet.streamChannel;
			
			if(_map[streamID].publish)
			{
				client.sendPacket(RtmpPacketManage.streamPublishStopMessage(responseID, packet.streamChannel));
				
				_map[streamID].streamName = "";
				_map[streamID].publish = false;
				_map[streamID].publishStreamID = 0;
				_map[streamID].playStreamIDs.splice(0, _map[streamID].playStreamIDs.length);
				
			}else{
				client.sendPacket(RtmpPacketManage.streamPlayStopMessage(responseID, packet.streamChannel));
				
				var publishStreamID:uint = _map[streamID].publishStreamID;
				if(!_map[publishStreamID]) return;
				var index:int = _map[publishStreamID].playStreamIDs.indexOf(streamID);
				if(index != -1) _map[publishStreamID].playStreamIDs.splice(index, 1);
			}
		}
		
		
		
		
		/**
		 * 删除视频 
		 * 
		 */ 
		
		public function onDeleteStream(packet:RtmpPacket):void
		{
			var responseID:uint = packet.readObject() as uint;
			var nullValue:Object = packet.readObject();
			var streamID:int = packet.readObject() as Number;
			
			if(_map[streamID])
			{
				_map[streamID] = null;
				delete _map[streamID];
			}
		}
	}
}